import os


class EnvConfig:

    ENV_MAP = {
        "xxxx-test": {
            "local": "http://nginx.xxxx-test.dq.yyyy-inc.com",
            "k8s": "http://nginx.xxxx-test",
            "db_ip": "dev-xxxx-db-e01.int.zzzzz.net",
            "db_port": "5503",
            "db_user": "ying99_testing",
            "db_password": "ying99_testing"
        },
        "xxxx-test2": {
            "local": "http://nginx.xxxx-test2.dq.yyyy-inc.com",
            "k8s": "http://nginx.xxxx-test2",
            "db_ip": "dev-xxxx-db-e01.int.zzzzz.net",
            "db_port": "5503",
            "db_user": "ying99_testing",
            "db_password": "ying99_testing"
        },
        "xxxx-test3": {
            "local": "http://nginx.xxxx-test3.dq.yyyy-inc.com",
            "k8s": "http://nginx.xxxx-test3",
            "db_ip": "dev-xxxx-db-e01.int.zzzzz.net",
            "db_port": "5503",
            "db_user": "ying99_testing",
            "db_password": "ying99_testing"
        },
        "xxxx": {
            "local": "http://nginx.xxxx.dq.yyyy-inc.com",
            "k8s": "http://nginx.xxxx",
            "db_ip": "dev-xxxx-db-e01.int.zzzzz.net",
            "db_port": "5503",
            "db_user": "ying99_testing",
            "db_password": "ying99_testing"
        }
    }
    DEFAULT_ENV = "xxxx-test3"

    def __init__(self):
        if os.environ.get('env') is None:
            os.environ['env'] = self.DEFAULT_ENV
        if os.environ.get('deploy') is None:
            os.environ['deploy'] = "local"

        self.env = os.environ.get('env')
        self.deploy = os.environ.get('deploy')
        self.base_url = self.ENV_MAP[self.env][self.deploy]
        self.tdp_url = self.ENV_MAP["xxxx"][self.deploy]

    def get_base_url(self, env: str = None) -> str:
        """
        获取指定环境的基础URL
        
        参数:
            env: 环境名称，默认使用当前环境
        返回:
            基础URL
        """
        target_env = env or self.env
        if target_env in self.ENV_MAP:
            return self.ENV_MAP[target_env][self.deploy]
        else:
            raise ValueError(f"未找到环境配置: {target_env}")

    def get_db_config(self, env: str = None) -> dict:
        """
        获取数据库配置信息
        
        参数:
            env: 环境名称，默认使用当前环境
        返回:
            数据库配置字典
        """
        target_env = env or self.env
        if target_env not in self.ENV_MAP:
            raise ValueError(f"未找到环境配置: {target_env}")
            
        env_data = self.ENV_MAP[target_env]
        return {
            "host": env_data.get("db_ip"),
            "port": int(env_data.get("db_port", 3306)),
            "user": env_data.get("db_user"),
            "password": env_data.get("db_password"),
            "database": None  # 可以在具体使用时指定
        }


env_config = EnvConfig()
